package com.xbongbong.saas.service.toolbox.statistic.help;

import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.customerstatement.pojo.StatementCustomerPojo;
import com.xbongbong.pro.statistic.result.pojo.ElasticAggsPojo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.CustomerStatementEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.rangeQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

@Component
public class ChartHelp {

    @Resource
    private EsHelper esHelper;

    /**
     * 获取单个数值总数
     * @param indexTypeEnum    索引，用于获取index和type
     * @param boolQueryBuilder 查询条件
     * @return int 总数
     * @author zcp
     * @date 2019/2/19 15:49
     * @since v1.0
     * @version v1.0
     */
    public int getCount4Total(IndexTypeEnum indexTypeEnum, BoolQueryBuilder boolQueryBuilder) throws XbbException {
        ElasticAggsPojo elasticAggsPojo = esHelper.elasticAggs(indexTypeEnum, boolQueryBuilder, null);
        return StringUtil.toInt(elasticAggsPojo.getTotal(), 0);
    }

    /**
     * 搜索时增加时间限制
     * @param boolQueryBuilder
     * @param checkingDate
     * @return org.elasticsearch.index.query.BoolQueryBuilder
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public BoolQueryBuilder statementQuery4CheckData(BoolQueryBuilder boolQueryBuilder, List<Long> checkingDate) {
        if (CollectionUtils.isEmpty(checkingDate)) {
            return boolQueryBuilder;
        }
        if (Objects.equals(checkingDate.size(), BasicConstant.TWO)) {
            //开始结束日期
            boolQueryBuilder.filter(rangeQuery(CustomerStatementEnum.getEsAttr(CustomerStatementEnum.CHECKING_DATE)).gte(checkingDate.get(0)).lt(checkingDate.get(1)));
        } else {
            //只限制开始时间
            boolQueryBuilder.filter(rangeQuery(CustomerStatementEnum.getEsAttr(CustomerStatementEnum.CHECKING_DATE)).gte(checkingDate.get(0)));
        }
        return boolQueryBuilder;
    }

    /**
     * 对账单查询条件封装：corpid、del、checkingDate(开始、结束)
     * @param corpid 公司id
     * @param statementCustomerPojo
     * @return org.elasticsearch.index.query.BoolQueryBuilder
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public BoolQueryBuilder statementQuery4CheckData(String corpid, StatementCustomerPojo statementCustomerPojo) {
        //corpid、del
        BoolQueryBuilder boolQueryBuilder = boolQueryBuilder4Base(corpid, DelEnum.NORMAL.getDel());
        //开始结束日期
        boolQueryBuilder.filter(rangeQuery(CustomerStatementEnum.getEsAttr(CustomerStatementEnum.CHECKING_DATE)).gte(statementCustomerPojo.getStartTime()).lt(statementCustomerPojo.getEndTime()));
        return boolQueryBuilder;
    }

    /**
     * 最基础的查询条件封装，只限制corpid和del（del为null时不限制）
     * @param corpid 公司id
     * @param del 删除标记@see DelEnum，为空时不限制删除条件
     * @return org.elasticsearch.index.query.BoolQueryBuilder
     * @author zcp
     * @date 2019/5/13 21:57
     * @since v1.0
     * @version v1.0
     */
    public BoolQueryBuilder boolQueryBuilder4Base(String corpid, Integer del){
        /*--------处理查询条件--------*/
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
        if (del != null) {
            boolQueryBuilder.filter(termQuery("del", del));
        }
        return boolQueryBuilder;
    }

    /**
     * 计算(对账表内)期初的条件封装：corpid、del、checkingDate(只限制截至到筛选日期)、客户id
     * @param corpid
     * @param statementCustomerPojo
     * @return org.elasticsearch.index.query.BoolQueryBuilder
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public BoolQueryBuilder statementQuery4Begining(String corpid, StatementCustomerPojo statementCustomerPojo) {
        //corpid、del
        BoolQueryBuilder boolQueryBuilder = boolQueryBuilder4Base(corpid, DelEnum.NORMAL.getDel());
        //限制开始时间：期初 = (截止至"对账日期"的开始日期所有)应收款总额  - 核销总金额
        Long startTime = statementCustomerPojo.getStartTime();
        //期初限制对账日期小于筛选开始日期
        boolQueryBuilder.filter(rangeQuery(CustomerStatementEnum.getEsAttr(CustomerStatementEnum.CHECKING_DATE)).lt(startTime));
        //限制客户id条件
        setCustomerIdFilter4Statement(statementCustomerPojo, boolQueryBuilder);
        return boolQueryBuilder;
    }

    /**
     * 限制对账单统计的客户id条件（单个对账表为一个customerId，导出、打印、总汇总计算为customerIdList）
     * @param statementCustomerPojo
     * @param boolQueryBuilder
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public void setCustomerIdFilter4Statement(StatementCustomerPojo statementCustomerPojo, BoolQueryBuilder boolQueryBuilder) {
        List<Long> customerIdList = statementCustomerPojo.getCustomerIdList();
        if (CollectionUtils.isNotEmpty(customerIdList)) {
            boolQueryBuilder.filter(termsQuery(CustomerStatementEnum.getEsAttr(CustomerStatementEnum.CUSTOMER_ID), customerIdList));
        } else {
            boolQueryBuilder.filter(termQuery(CustomerStatementEnum.getEsAttr(CustomerStatementEnum.CUSTOMER_ID), statementCustomerPojo.getCustomerId()));
        }
    }
}
